﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UCS.XNA.Utils
{
    public class AnimatedSprite
    {
        #region Private Members

        private Texture2D _texture;
        private SpriteBatch _spriteBatch;
        private List<Rectangle> _imagesBounds;
        private Rectangle? _currentBounds;
        private int _miliseconds;

        #endregion

        #region Attributes and Properties

        private Rectangle _bounds;
        public Rectangle Bounds
        {
            get { return this._bounds; }
            set { this._bounds = value; }
        }

        #endregion

        #region Events

        public event EventHandler<EventArgs> AnimationFinished;

        #endregion

        #region Contructors

        public AnimatedSprite(Texture2D texture, Rectangle bounds, int imageCount, int imageWidth, int imageHeight, SpriteBatch spriteBatch)
        {
            this._texture = texture;
            this._spriteBatch = spriteBatch;
            this._bounds = bounds;

            this.InitializeImagesBounds(imageCount, imageWidth, imageHeight);
        }

        #endregion

        #region Private Methods

        private void InitializeImagesBounds(int imageCount, int imageWidth, int imageHeight)
        {
            this._imagesBounds = new List<Rectangle>();

            for (int i = 0; i < this._texture.Width / imageWidth; i++)
            {
                Rectangle rectangle = new Rectangle();
                rectangle.X = i * imageWidth;
                rectangle.Y = 0;
                rectangle.Width = imageWidth;
                rectangle.Height = imageHeight;

                this._imagesBounds.Add(rectangle);
            }
        }

        #endregion

        #region Public Methods

        public void Start()
        {
            if (this._imagesBounds.Count > 0)
                this._currentBounds = this._imagesBounds.First();
        }

        public void Update(GameTime gameTime)
        {
            this._miliseconds += gameTime.ElapsedGameTime.Milliseconds;
            if (this._miliseconds > 30)
            {
                this._miliseconds = 0;
                if (this._currentBounds.HasValue)
                {
                    if (this._currentBounds == this._imagesBounds.Last())
                    {
                        this._currentBounds = null;
                        if (this.AnimationFinished != null)
                            this.AnimationFinished(this, new EventArgs());
                    }
                    else
                        this._currentBounds = this._imagesBounds[this._imagesBounds.IndexOf(this._currentBounds.Value) + 1];
                }
            }
        }

        public void Draw(GameTime gameTime)
        {
            if (this._currentBounds.HasValue)
            {
                this._spriteBatch.Begin();

                this._spriteBatch.Draw(this._texture, this._bounds, this._currentBounds, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);

                this._spriteBatch.End();
            }
        }

        #endregion
    }
}
